﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FizzBuzz
{
    internal class EventsDemo
    {
        public void Run()
        {
            Stock s = new Stock("MSFT");
            s.Price = 27.1M;
            s.PriceChanged += PriceChangedHandler;
            s.Price = 32.1M;
        }

        public void PriceChangedHandler(object sender, PriceChangedEventArgs e)
        {
            Console.WriteLine("Stock Price- Old Price:{0}, New Price:{1}", e.LastPrice, e.NewPrice);
        }
    }

    internal class Stock
    {
        private string symbol;
        private decimal price;

        public event EventHandler<PriceChangedEventArgs> PriceChanged;

        public Stock(string symbol)
        {
            this.symbol = symbol;
        }

        public decimal Price
        {
            get { return price; }
            set
            {
                if (price == value) return;
                OnPriceChanged(new PriceChangedEventArgs(price, value));

                price = value;
            }
        }

        public virtual void OnPriceChanged(PriceChangedEventArgs e)
        {
            if (PriceChanged != null)
            {
                PriceChanged(this, e);
            }
        }

//        public event PriceChangedHandler 
    }

    public class PriceChangedEventArgs : System.EventArgs
    {
        /// <summary>
        ///     Use this constructor to initialize the event arguments
        ///     object with the custom event fields
        /// </summary>
        public PriceChangedEventArgs(decimal lastPrice, decimal newPrice)
        {
            this.LastPrice = lastPrice;
            this.NewPrice = newPrice;
        }

        public decimal LastPrice
        {
            get;
            set;
        }

        public decimal NewPrice
        {
            get;
            set;
        }
    }
}